package com.yxt.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yxt.data.UserMapper;
import com.yxt.model.ChatHis;
import com.yxt.model.UserInfo;


@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	
	@Transactional(rollbackFor=Exception.class)
	public void addUserInfo(Map<String,Object> params) throws Exception {
		Date now = new Date();
		params.put("register_date", now);
		params.put("last_login_date", now);
		params.put("login_date", now);
		//添加注册信息
		userMapper.addUserInfo(params);	
		//添加登录历史
		userMapper.addLoginHis(params);
	}
	
	@Transactional(rollbackFor=Exception.class)
	public boolean verifyUserInfo(Map<String,Object> params) {
		//根据用户名、密码查找用户
		List<UserInfo> userList = userMapper.getUser(params);
		Date now = new Date();
		params.put("last_login_date", now);
		params.put("login_date", now);
		if(userList.size() > 0) {
			//更新登录时间及ip
			userMapper.updateLoginDate(params);
			//添加登陆历史
			userMapper.addLoginHis(params);
			//把name也放到params里返回客户端
			params.put("name", userList.get(0).getName());
			return true;
		} else {
			return false;
		}
	}
	
	
	public List<UserInfo> getOnlineList(Map<String,Object> params) {
		return userMapper.getOnlineList(params);
	}
	
	public void setOnline(Map<String,Object> params) {
		userMapper.setOnline(params);
	}
	
}
